package net.wanho.shop.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置类
 */
@Configuration
@EnableAsync //  启用异步任务
public class ThreadPoolConfig {

    @Bean
    public Executor taskExecutor() {
        // 定义线程池，使用Spring提供的ThreadPoolTaskExecutor
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10); // 设置核心线程数
        executor.setMaxPoolSize(20); // 设置最大线程数
        executor.setQueueCapacity(50); // 设置缓冲列表大小
        executor.setKeepAliveSeconds(60); // 设置线程空闲时间
        executor.setThreadNamePrefix("MyExecutor-"); // 设置线程名前缀
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝处理策略
        return executor;

    }

}